package com.qianfeng.front.controller;

import com.qianfeng.front.biz.IGoodsBiz;
import com.qianfeng.ls.entity.GoodsEntity;
import com.qianfeng.ls.entity.ResponseResult;
import com.qianfeng.ls.finalstr.FruitsFinal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

@RestController //这个类里面所有的方法都是处理ajax请求
public class ShopCarController {

    @Autowired
    IGoodsBiz goodsBiz;

    //添加商品到购物车
    @RequestMapping("addShopCar")
    public ResponseResult addShopCar(String gid, HttpSession session){

        ResponseResult rr = new ResponseResult();

        //添加到购物车;

        // 1 : 得到自己的购物车  商品id(key) 商品信息(value)
        Map<String, GoodsEntity> shopCar = (Map<String, GoodsEntity>) session.getAttribute("shopCar");

        if(null == shopCar){//说明第一次往购物车里面添加东西;
            shopCar = new HashMap<>(); //没有购物车,new一个购物车;
        }

        String thiscount="";//保存的就是当前商品的总价格

        //2: 往购物车里面添加商品了; 两种情况[第一次添加这个商品][之前已经加过这个商品了,只要对商品的数量+1就行了]

        if(null == shopCar.get(gid)){ //如果当前商品是第一次添加
            //2.1 查询到这个商品
            GoodsEntity goodsEntity = goodsBiz.queryGoodsDetail(gid);
            //2.2加到购物车
            shopCar.put(gid,goodsEntity);

            thiscount+= goodsEntity.getGoodsPrice();

        }else{ //存在这个商品,直接对数量进行+1
            GoodsEntity goodsEntity = shopCar.get(gid);
            goodsEntity.setGoodsNumber(goodsEntity.getGoodsNumber()+1);

            thiscount += goodsEntity.getGoodsPrice()*goodsEntity.getGoodsNumber();
        }

        //3: 保存购物车;
        session.setAttribute("shopCar",shopCar);

        //4: 把当前商品的数量,当前商品的总价格;返回到前台展示;  当前操作商品的总价

        rr.setResult(FruitsFinal.SUCCESS_RESULT);
        rr.setResultMsg(getNumberAndPrirce(shopCar)+","+thiscount);
        return rr;
    }

    //从购物车移除（减掉）商品
    @RequestMapping("subShopCar")
    public ResponseResult subShopCar(String gid, HttpSession session){
        ResponseResult rr = new ResponseResult();

        //1:获取购物车
        Map<String, GoodsEntity> shopCar = (Map<String, GoodsEntity>)session.getAttribute("shopCar");

        GoodsEntity ge = shopCar.get(gid);//从购物车获取该商品

        if(null == ge){
            rr.setResult(FruitsFinal.NAME_NULL_RESULT); //非法操作
        }

        //2.1:从购物车数量大于1，这个商品的数量-1
        String thiscount = "";
        if(ge.getGoodsNumber() > 1){

            ge.setGoodsNumber(ge.getGoodsNumber()-1);
            thiscount += ge.getGoodsNumber() * ge.getGoodsPrice();

            rr.setResult("9000");//代表操作成功，并且数量-1

        }else{//2.2:数量等于1 ，把这个商品从购物车移除

            shopCar.remove(gid);// shopCar.remove(ge);是否可以使用？
            rr.setResult("9001"); //操作成功，移除商品
        }

        String count = getNumberAndPrirce(shopCar);//获取购物车的总数量和总价格；

        rr.setResultMsg(count+","+thiscount);

        //将购物车回填
        session.setAttribute("shopCar",shopCar);

        return rr;
    }

    //得到商品的数量和总价格
    private String getNumberAndPrirce(Map<String, GoodsEntity> shopCar){

        if(null == shopCar){
            return "0,0.00";
        }

        Iterator<GoodsEntity> iterator = shopCar.values().iterator();

        GoodsEntity ge;
        int numberCount = 0; //商品数量
        float priceCount = 0; //商品总价
        while(iterator.hasNext()){
            ge = iterator.next();
            numberCount += ge.getGoodsNumber();

            priceCount += ge.getGoodsNumber() * ge.getGoodsPrice();
        }

        return numberCount+","+priceCount;
    }

}
